package com.androidhive.dashboard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import androidhive.dashboard.R;

public class View_Concept_Activity extends Activity 
{
	private ProgressDialog pDialog;
	public static Boolean isOpen=false;
	
	private EditText et_cs_products;
	private EditText et_cs_market;
	private EditText et_cs_prodbenefits;
	private EditText et_cs_proddistri;
	
	private EditText et_bp_overview;
	private EditText et_bp_product;
	private EditText et_bp_marketa;
	private EditText et_bp_markets;
	private EditText et_bp_finances;
	private EditText et_bp_operations;
	
	private EditText et_fm_budget;
	
	private CheckBox cb_fm_analysis, cb_fm_budgeting, cb_fm_bookkeeping;
	private CheckBox cb_tr_bmt, cb_tr_pmt, cb_tr_fmt, cb_tr_hrt, cb_tr_spt;
	
	// Creating JSON Parser object
	JSONParser jParser = new JSONParser();

	ArrayList<HashMap<String, String>> emailsList;

	// url to get all products list
	private static String url_all_products = "http://i-com.co.ug/ufollo/query_multiple.php";

	// JSON Node names
	private static final String TAG_SUCCESS = "success";
	private static final String TAG_PRODUCTS = "seda_teams";
	private static final String TAG_PID = "team_name";
	private static final String TAG_prod_desc = "prod_desc";
	private static final String TAG_target_market = "target_market";
	private static final String TAG_product_benefits = "product_benefits";
	private static final String TAG_product_distribution = "product_distribution";
	private static final String TAG_business_overview = "business_overview";
	private static final String TAG_product_service = "product_service";
	private static final String TAG_market_analysis = "market_analysis";
	private static final String TAG_market_strategy = "market_strategy";
	private static final String TAG_finances = "finances";
	private static final String TAG_operations = "operations";
	private static final String TAG_budgeting_desc = "budgeting_desc";
	private static final String TAG_financial_analysis = "financial_analysis";
	private static final String TAG_budgeting = "budgeting";
	private static final String TAG_bookkeeping = "bookkeeping";
	private static final String TAG_business_mgt = "business_mgt";
	private static final String TAG_project_mgt = "project_mgt";
	private static final String TAG_financial_mgt = "financial_mgt";
	private static final String TAG_hrm_training = "hrm_training";
	private static final String TAG_special_training = "special_training";
	
	String name;
	// products JSONArray
	JSONArray inbox_messages = null;
	
	 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_combined_concept);
        
        Intent intent = getIntent();
        name = intent.getStringExtra("team_name");
        
        et_cs_products = (EditText) findViewById(R.id.et_cs_products);
        et_cs_market = (EditText) findViewById(R.id.et_cs_market);
        et_cs_prodbenefits = (EditText) findViewById(R.id.et_cs_prodbenefits);
        et_cs_proddistri = (EditText) findViewById(R.id.et_cs_proddistri);
        
        et_bp_overview = (EditText) findViewById(R.id.et_bp_overview);
    	et_bp_product = (EditText) findViewById(R.id.et_bp_product);
    	et_bp_marketa = (EditText) findViewById(R.id.et_bp_marketa);
    	et_bp_markets = (EditText) findViewById(R.id.et_bp_markets);
    	et_bp_finances = (EditText) findViewById(R.id.et_bp_finances);
    	et_bp_operations = (EditText) findViewById(R.id.et_bp_operations);
    	
    	et_fm_budget = (EditText) findViewById(R.id.et_fm_budget);
    	
    	cb_fm_analysis = (CheckBox) findViewById(R.id.cb_fm_analysis);
    	cb_fm_budgeting = (CheckBox) findViewById(R.id.cb_fm_budgeting);
    	cb_fm_bookkeeping = (CheckBox) findViewById(R.id.cb_fm_bookkeeping);
    	
    	cb_tr_bmt = (CheckBox) findViewById(R.id.cb_tr_bmt);
    	cb_tr_pmt = (CheckBox) findViewById(R.id.cb_tr_pmt);
    	cb_tr_fmt = (CheckBox) findViewById(R.id.cb_tr_fmt);
    	cb_tr_hrt = (CheckBox) findViewById(R.id.cb_tr_hrt);
    	cb_tr_spt = (CheckBox) findViewById(R.id.cb_tr_spt);
        
        // Hashmap for ListView
		emailsList = new ArrayList<HashMap<String, String>>();

		// Loading products in Background Thread
		new LoadAllProducts().execute();

	
    }
    
    /**
	 * Background Async Task to Load all product by making HTTP Request
	 * */
	class LoadAllProducts extends AsyncTask<String, String, String> {

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(View_Concept_Activity.this);
			pDialog.setMessage("Loading client concepts et al... Please wait...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
		}

		/**
		 * getting All products from url
		 * */
		protected String doInBackground(String... args) {
			
			String query_variable = name;
			// Building Parameters
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("team_name", query_variable));
			// getting JSON string from URL
			JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
			
			// Check your log cat for JSON reponse
			Log.d("All Client Business Data: ", json.toString());

			try {
				// Checking for SUCCESS TAG
				int success = json.getInt(TAG_SUCCESS);

				if (success == 1) {
					// products found
					// Getting Array of Products
					inbox_messages = json.getJSONArray(TAG_PRODUCTS);

					// looping through All Products
					for (int i = 0; i < inbox_messages.length(); i++) {
						JSONObject c = inbox_messages.getJSONObject(i);

						// Storing each json item in variable
						String id = c.getString(TAG_PID);
						String a = c.getString(TAG_prod_desc);
						String b = c.getString(TAG_target_market);
						String d = c.getString(TAG_product_benefits);
						String e = c.getString(TAG_product_distribution);
						String f = c.getString(TAG_business_overview);
						String g = c.getString(TAG_product_service);
						String h = c.getString(TAG_market_analysis);
						String i2 = c.getString(TAG_market_strategy);
						String j = c.getString(TAG_finances);
						String k = c.getString(TAG_operations);
						String l = c.getString(TAG_budgeting_desc);
						String m = c.getString(TAG_financial_analysis);
						String n = c.getString(TAG_budgeting);
						String o = c.getString(TAG_bookkeeping);
						String p = c.getString(TAG_business_mgt);
						String q = c.getString(TAG_project_mgt);
						String r = c.getString(TAG_financial_mgt);
						String s = c.getString(TAG_hrm_training);
						String t = c.getString(TAG_special_training);
						

						// creating new HashMap
						HashMap<String, String> map = new HashMap<String, String>();

						// adding each child node to HashMap key => value
						map.put(TAG_PID, id);
						map.put(TAG_prod_desc, a);
						map.put(TAG_target_market, b);
						map.put(TAG_product_benefits, d);
						map.put(TAG_product_distribution, e);
						map.put(TAG_business_overview, f);
						map.put(TAG_product_service, g);
						map.put(TAG_market_analysis, h);
						map.put(TAG_market_strategy, i2);
						map.put(TAG_finances, j);
						map.put(TAG_operations, k);
						map.put(TAG_budgeting_desc, l);
						map.put(TAG_financial_analysis, m);
						map.put(TAG_budgeting, n);
						map.put(TAG_bookkeeping, o);
						map.put(TAG_business_mgt, p);
						map.put(TAG_project_mgt, q);
						map.put(TAG_financial_mgt, r);
						map.put(TAG_hrm_training, s);
						map.put(TAG_special_training, t);

						// adding HashList to ArrayList
						emailsList.add(map);
					}
				} 
				else 
				{
					// no products found
					// Launch Add New product Activity
					Intent i = new Intent(getApplicationContext(),
							View_Concept_Activity.class);
					// Closing all previous activities
					i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(i);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

			return null;
		}

		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		protected void onPostExecute(String file_url) 
		{
			// dismiss the dialog after getting all products
			pDialog.dismiss();
			// updating UI from Background Thread
			et_cs_products.setText(emailsList.get(0).get(TAG_prod_desc));
			et_cs_market.setText(emailsList.get(0).get(TAG_target_market));
			et_cs_prodbenefits.setText(emailsList.get(0).get(TAG_product_benefits));
			et_cs_proddistri.setText(emailsList.get(0).get(TAG_product_distribution));
			et_bp_overview.setText(emailsList.get(0).get(TAG_business_overview));
			et_bp_product.setText(emailsList.get(0).get(TAG_product_service));
			et_bp_marketa.setText(emailsList.get(0).get(TAG_market_analysis));
			et_bp_markets.setText(emailsList.get(0).get(TAG_market_strategy));
			et_bp_finances.setText(emailsList.get(0).get(TAG_finances));
			et_bp_operations.setText(emailsList.get(0).get(TAG_operations));
			et_fm_budget.setText(emailsList.get(0).get(TAG_budgeting_desc));
			
			if(emailsList.get(0).get(TAG_financial_analysis).equals("1"))
			{
				cb_fm_analysis.setChecked(true);
			}
			
			if(emailsList.get(0).get(TAG_budgeting) == "1")
			{
				cb_fm_budgeting.setChecked(true);
			}
			
			if(emailsList.get(0).get(TAG_bookkeeping) == "1")
			{
				cb_fm_bookkeeping.setChecked(true);
			}
			
			if(emailsList.get(0).get(TAG_business_mgt) == "1")
			{
				cb_tr_bmt.setChecked(true);
			}
			
			if(emailsList.get(0).get(TAG_project_mgt) == "1")
			{
				cb_tr_pmt.setChecked(true);
			}
			
			if(emailsList.get(0).get(TAG_financial_mgt) == "1")
			{
				cb_tr_fmt.setChecked(true);
			}
			
			if(emailsList.get(0).get(TAG_hrm_training) == "1")
			{
				cb_tr_hrt.setChecked(true);
			}
			
			if(emailsList.get(0).get(TAG_special_training) == "1")
			{
				cb_tr_spt.setChecked(true);
			}


		}

	}
}
